#include "func.h"

int main(int argc, char *argv[])
{
    ARGS_CHECK(argc, 3);
    int client_fd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(client_fd, -1, "socket");
    struct sockaddr_in serAddr;
    serAddr.sin_family = AF_INET;
    serAddr.sin_addr.s_addr = inet_addr(argv[1]);
    serAddr.sin_port = htons(atoi(argv[2]));
    int ret;
    ret = connect(client_fd, (struct sockaddr *)&serAddr, sizeof(serAddr));
    ERROR_CHECK(ret, -1, "connect");

    // 询问注册还是登录
    int control_code;
start:
    printf("1:注册,2:登录\n");
    scanf("%d", &control_code);
    username_passwd_t user_passwd;
    // 注册
    if (1 == control_code)
    {
        input_username_passwd(&user_passwd);
        send_protocal(client_fd, control_code, &user_passwd, sizeof(user_passwd));
        recv_protocal(client_fd, &control_code, NULL);
        if (control_code == REGISTER_FAILED)
        {
            goto start;
        }
        printf("注册成功，已登录\n");
    }
    else if (2 == control_code) // 登录
    {
        input_username_passwd(&user_passwd);
        send_protocal(client_fd, control_code, &user_passwd, sizeof(user_passwd));
        // printf("发送成功\n");
        recv_protocal(client_fd, &control_code, NULL);
        if (control_code == LOGIN_FAILED)
        {
            goto start;
        }
        printf("登录成功\n");
    }
    else
    {
        printf("错误输入\n");
        goto start;
    }
    getchar(); // 去除换行
    // 这里是登录了,开始发各种请求
    char cmd[1000];
    char path[1000]; // 各个命令后面带的参数存储
    while (1)
    {
        fgets(cmd, sizeof(cmd), stdin); // 读取命令
        printf("cmd=%s\n", cmd);
        cmd[strlen(cmd) - 1] = 0; // 去除\n
        system("clear");
        if (strncmp("cd", cmd, 2) == 0)
        {
            // cmd+3就是cd后面带的文件夹名字
            send_protocal(client_fd, CD, cmd + 3, strlen(cmd) - 3);
            bzero(path, sizeof(path));
            recv_protocal(client_fd, &control_code, path); // 收到成功还是失败
            if (SUCCESS == control_code)
            {
                printf("%s\n", path);
            }
            else
            {
                printf("文件夹不存在，切换失败\n");
            }
        }
        else if (strncmp("ls", cmd, 2) == 0)
        {
            send_protocal(client_fd, LS, NULL, 0);
            int recvLen;
            char buf[1024];
            while (1)
            {
                recvn(client_fd, &recvLen, 4); // 接火车车厢
                if (recvLen > 0)
                {
                    bzero(buf,sizeof(buf));
                    recvn(client_fd, buf, recvLen); // 接收一行
                    puts(buf);     //打印一行
                }
                else
                {
                    break;
                }
            }
        }
        else if (strncmp("puts", cmd, 4) == 0)
        {
            //发送控制码，文件名
            send_protocal(client_fd, PUTS, cmd + 5, strlen(cmd) - 5);
            //获取文件的md5值并发送
            char md5_str[50]={0};
            Compute_file_md5(cmd+5,md5_str);
            printf("%s\n",md5_str);
            train_t t;
            t.size=strlen(md5_str);
            strcpy(t.buf,md5_str);
            send(client_fd,&t,4+t.size,0);
            //判断是否秒传成功
            recv_protocal(client_fd, &control_code, NULL); // 收到成功还是失败
            if (SUCCESS == control_code)
            {
                printf("秒传成功\n");
            }
            else
            {
                printf("秒传失败\n");
                transFile(client_fd,cmd + 5);
                printf("发送成功\n");
            }

        }
        else if (strncmp("gets", cmd, 4) == 0)
        {
            off_t seek_pos;
            struct stat file_buf;
            
            int fd=open(cmd+5,O_RDONLY);
            if(-1==fd)
            {
                printf("文件不存在\n");
                seek_pos=0;
            }else{
                fstat(fd,&file_buf);
                seek_pos=file_buf.st_size;//本地存在，下载了一部分
                close(fd);
            }
            char new_cmd[1200]={0};
            sprintf(new_cmd,"%s %ld",cmd,seek_pos);
            send_protocal(client_fd, GETS, new_cmd + 5, strlen(new_cmd) - 5);
            recvFile(client_fd,cmd+5);//接文件
        }
        else if (strncmp("rm", cmd, 2) == 0)
        {
            send_protocal(client_fd, RM, cmd + 3, strlen(cmd) - 3);
            recv_protocal(client_fd, &control_code, NULL); // 收到成功还是失败
            if (SUCCESS == control_code)
            {
                printf("删除成功\n");
            }
            else
            {
                printf("目录非空，失败\n");
            }
        }
        else if (strncmp("pwd", cmd, 3) == 0)
        {
            send_protocal(client_fd, PWD, NULL, 0); // 发送pwd请求
            bzero(path, sizeof(path));
            recv_protocal(client_fd, &control_code, path);
            printf("%s\n", path); // 输出path
        }
        else if (strncmp("mkdir", cmd, 5) == 0)
        {
            // cmd+6就是mkdir后面带的文件夹名字
            send_protocal(client_fd, MKDIR, cmd + 6, strlen(cmd) - 6);
            recv_protocal(client_fd, &control_code, NULL); // 收到成功还是失败
            if (SUCCESS == control_code)
            {
                printf("创建成功\n");
            }
            else
            {
                printf("文件夹存在，创建失败\n");
            }
        }
        else
        {
            continue;
        }
    }
    close(client_fd);
    return 0;
}